//author:kakake
class tsbookController {
    public dbConn = TSLobsterAPI.Program.GetDbConn();

    public bookModel = new Model().Books;

    getdata() {
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };


        this.bookModel.where = TSLobsterAPI.DbHelper.GetWhere(this.bookModel.query);
        this.bookModel.pageinfo = pageInfo;

        TSLobsterAPI.Utils.TrackLog("where", this.bookModel.where);
        TSLobsterAPI.Utils.TrackLog("WorkId", this.bookModel.WorkId);

        var job = TSLobsterAPI.AppFunction.GetCustomFactory("Lobster.Quartz.IJobService").GetActiveJob();

        var datagrid = { data: {}, count: 0, job: job ,date:TSLobsterAPI.Utils.dateFormat(new Date())};
        datagrid.data = TSLobsterAPI.DbHelper.GetListModelPage(this.dbConn, this.bookModel);
        datagrid.count = pageInfo.totalRecord;

        //TSLobsterAPI.Utils.TrackLog("datagrid",datagrid);

        return datagrid;
    }

    testsys() {
        let workid = TSLobsterAPI.Program.GetWorkId();
        let appid = TSLobsterAPI.Program.GetAppId();
        let ret = TSLobsterAPI.AppFunction.GetSystemConfigs(workid, appid, 'SubMchId');
    }

    getdata2() {
        var pageInfo = {
            key: "Id",
            page: TSLobsterAPI.Program.RequestQuery("page", "1"),
            perPage: TSLobsterAPI.Program.RequestQuery("limit", "10"),
            totalRecord: 100
        };
        //var strsql="select * from Books where flag=? and workid=?";
        var strsql = "select * from Books";
        var param = null;
        // param.push(0);
        // param.push(TSLobsterAPI.Program.GetWorkId());
        TSLobsterAPI.Utils.TrackLog("strsql", strsql);
        TSLobsterAPI.Utils.TrackLog("WorkId", param);
        var data = TSLobsterAPI.DbHelper.DbGetJsonData(this.dbConn, strsql, param, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    }

    savemodel() {
        let self = this;
        self.bookModel.objval = JSON.parse(TSLobsterAPI.Program.RequestBody());

        if (self.existbookname(self.bookModel.objval.BookName) && self.bookModel.objval.Id == 0) {
            throw '请不要添加重复的书籍名称';
        }

        TSLobsterAPI.Utils.TrackLog("TableModel", self.bookModel);
        TSLobsterAPI.DbHelper.SaveModel(self.dbConn, self.bookModel);
        return { result: true };
    }

    deletemodel() {
        let self = this;
        //self.TableModel.val = TSLobsterAPI.Program.RequestQuery("Id", "0");
        //TSLobsterAPI.DbHelper.DeleteModel(self.data.dbConn, self.TableModel);
        var id = TSLobsterAPI.Program.RequestQuery("Id", "0");
        self.bookModel.val = id;

        var book = TSLobsterAPI.DbHelper.GetModel(self.dbConn, self.bookModel)
        var strsql;
        if (book.Flag == 0) {
            strsql = "update books set flag=1 where id=@id";
        } else {
            strsql = "update books set flag=0 where id=@id";
        }
        TSLobsterAPI.DbHelper.DbExecuteUpdate(self.dbConn, strsql, { id: id });
        return { result: true };
    }

    existbookname(bookname: string) {
        let self = this;
        let strsql = "select count(1) ct from Books where bookname=@bookname"
        var dbparams = { bookname: bookname };
        var res = TSLobsterAPI.DbHelper.DbGetJsonObject(self.dbConn, strsql, dbparams);
        if (res.ct > 0) {
            return true;
        } else {
            return false;
        }
    }
}